#include "widget.h"
#include "piece.h"

#define MENUBAR_HEIGHT 25

//Implementation of main widget(the game widget)
/* layout:
 * 1)the default height of menubar is 25 pixels
 * 2)the coordinate of top left point of the board is (80, 140)
 * 3)the coordinate of bottom left point of the board is (80, 780)
 * 4)length of 1 grid is 80 pixels
*/

//important coordinates alias:
//blt: board's left top point
//brb: board's right bottom point
QPoint blt((WIDGET_WIDTH - GL * 8) / 2,
    (WIDGET_HEIGHT - MENUBAR_HEIGHT - GL * 8) / 2 + 15);


//constructor
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
    //basic configuration
    setFixedSize(WIDGET_WIDTH, WIDGET_HEIGHT);
    setWindowTitle("Chess");
    setWindowIcon(QIcon(":/img/Icon.png"));

    //menubar
    QLayout* vertical = new QVBoxLayout(this);

    QMenuBar* menuBar = new QMenuBar(this);

    QMenu* menuOptions = menuBar->addMenu("options");
    QAction* actionNew = menuOptions->addAction("new game");
    QAction* actionResign = menuOptions->addAction("resign");
    QAction* actionDraw = menuOptions->addAction("offer draw");
    QAction* actionClose = menuOptions->addAction("close");

    QMenu* menuSettings = menuBar->addMenu("settings");
    QMenu* menuLanguage = menuSettings->addMenu("language");
    QAction* actionChinese = menuLanguage->addAction("中文");
    QAction* actionEnglish = menuLanguage->addAction("English");

    menuSettings->addAction("board type");
    menuSettings->addAction("manual");

    vertical->setMenuBar(menuBar);


    //place pieces, prepare to start a new game
    //create white pieces
    pieces.push_back(shared_ptr<Piece>(new Piece(0, "rook", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(0, "knight", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(0, "bishop", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(0, "queen", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(0, "king", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(0, "bishop", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(0, "knight", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(0, "rook", this)));

    for(int i = 0; i < 8; i++){
        pieces.push_back(shared_ptr<Piece>(new Piece(0, "pawn", this)));
    }

    //create black pieces
    pieces.push_back(shared_ptr<Piece>(new Piece(1, "rook", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(1, "knight", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(1, "bishop", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(1, "queen", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(1, "king", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(1, "bishop", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(1, "knight", this)));
    pieces.push_back(shared_ptr<Piece>(new Piece(1, "rook", this)));

    for(int i = 0; i < 8; i++){
        pieces.push_back(shared_ptr<Piece>(new Piece(1, "pawn", this)));
    }


    //assign unique identification for each piece
    int n = 0;
    for(shared_ptr<Piece> p : pieces){
        p->setNumber(n);

        if(p->type == "rook") p->score = 5;
        if(p->type == "knight" || p->type == "bishop") p->score = 3;
        if(p->type == "queen") p->score = 9;
        if(p->type == "pawn") p->score = 1;
        if(p->type == "king") p->score = 0;

        ++n;
    }

    //set original location for each pieces
    for(size_t i = 0; i < pieces.size(); i++){
        int line_control = 0;

        if(pieces[i]->type == "pawn"){
            pieces[i]->color == 0 ? line_control = 2 : line_control = 7;
        } else {
            pieces[i]->color == 0 ? line_control = 1 : line_control = 8;
        }

        pieces[i]->setGeometry(blt.x() + GL * (i % 8), blt.y() + BOARD_HEIGHT - GL * line_control, GL, GL);
    }
}

Widget::~Widget()
{
}

void Widget::paintEvent(QPaintEvent *event){
    QPainter p(this);

    QPixmap deep(":/img/deep.png");
    QPixmap shallow(":/img/shallow.png");

    deep = deep.scaled(deep.width() * GL * 0.01, deep.height() * GL * 0.01);
    shallow = shallow.scaled(shallow.width() * GL * 0.01, shallow.height() * GL * 0.01);

    //paint the board
    for(int i = 0; i < 8; i++){
        for(int j = 0; j < 8; j++){
            QPixmap grid;
            (i + j) % 2 == 0 ? grid = shallow : grid = deep;
            p.drawPixmap(blt.x() + i * GL, blt.y() + j * GL, grid.width(), grid.height(), grid);
        }
    }

    //TODO: paint the coordinates

    return QWidget::paintEvent(event);
}
